package com.chuanjiabao.wisdom.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.databinding.ActivityInputConfirmSecretPwdBinding;
import com.chuanjiabao.wisdom.ui.ActivityStack;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.chuanjiabao.wisdom.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

public class InputConfirmSecretPwdActivity extends BaseActivity {

    private ActivityInputConfirmSecretPwdBinding binding;
    private String pwdType, pwdOld, pwdNew;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_input_confirm_secret_pwd);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        pwdType = getIntent().getStringExtra("module_type");
        pwdOld = getIntent().getStringExtra("module_id");
        pwdNew = getIntent().getStringExtra("module_new");
        binding.tvModuleOk.setOnClickListener(this);
        binding.ivModuleDelete.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivModuleDelete:
                finish();
                break;
            case R.id.tvModuleOk:
                String pwd = binding.etModulePwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast("请输入确认密码");
                    return;
                }

                if (!TextUtils.equals(pwdNew, pwd)) {
                    ToastUtil.showToast("密码不一致，请重新输入");
                    return;
                }

                if (TextUtils.equals("reset", pwdType)) {
                    resetSecretPwd(pwdOld, pwdNew, pwd);
                } else {
                    changeSecretPwd(pwdOld, pwdNew, pwd);
                }
                break;
            default:
        }
    }

    private void resetSecretPwd(String bodyNum, String pwd, String confirm) {
        HTTPAPI.getInstance().resetSecretPwd(bodyNum, pwd, confirm, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(InputConfirmSecretPwdActivity.this, s);
                } else {
                    ToastUtil.showToast("密码重置成功");
                    ActivityStack.getScreenManager().popActivities(
                            InputConfirmSecretPwdActivity.class,
                            InputNewSecretPwdActivity.class
                    );
                }
            }
        });
    }

    private void changeSecretPwd(String old, String pwd, String confirm) {
        HTTPAPI.getInstance().changeSecretPwd(old, pwd, confirm, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(InputConfirmSecretPwdActivity.this, s);
                } else {
                    ToastUtil.showToast("密码修改成功");
                    ActivityStack.getScreenManager().popActivities(
                            InputConfirmSecretPwdActivity.class,
                            ChangeNewSecretPwdActivity.class
                    );
                }
            }
        });
    }

}
